package com.hz.airline.task.data;

import com.hz.airline.dao.*;
import com.hz.airline.entity.*;
import com.hz.airline.task.BaseServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by kevin on 2020/3/10 14:06
 */
@RestController
public class DataServlet extends BaseServlet {

    @Autowired
    private FlightPlayDao flightPlayDao;

    @Autowired
    private FlightInfomationDao flightInfomationDao;

    @Autowired
    private SpecialTicketDao specialTicketDao;

    @Autowired
    private SystemTipDao systemTipDao;

    @Autowired
    private UserMemberZoneDao userMemberZoneDao;

    // 日期
    private Calendar calendar = Calendar.getInstance();
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    // 城市
    private String[] citys = {"上海", "北京", "深圳", "杭州", "安阳"};
    // 航班计划
    private String[] flights = {"C100", "C101", "C102", "C103", "C104", "C105", "C106", "C107", "C108", "C109", "C110", "C111"};

    @RequestMapping(value = "/flightplayInfomation", method = RequestMethod.GET)
    @ResponseBody
    public String createFlightInfomation() {
        // 索引
        int index = 1;
        // 航班状态
        String[] status = {"已取消", "未起飞", "已起飞"};
        // 航班状态
        try {
            // 清空库
            flightInfomationDao.deleteAll();
            // 日期
            for (int i = 0; i < 5; i++) {
                calendar.setTime(new Date());
                calendar.add(Calendar.DATE, i);
                // 信息
                for (int k = 0; k < flights.length; k++) {
                    // 赋值
                    FlightInfomation infomation = new FlightInfomation();
                    infomation.setFlightnumberid(flights[k]);
                    infomation.setDepartdate(calendar.getTime());
                    infomation.setStatus(status[new Random().nextInt(2)]);
                    flightInfomationDao.save(infomation);
                    // 增加
                    index++;
                }
            }
        } catch (Exception e) {
            return "失败:" + e.getMessage();
        }
        return "成功插入：" + index + "数据";
    }

    @RequestMapping(value = "/flightplay", method = RequestMethod.GET)
    @ResponseBody
    public String createFlightPlay() {
        // 索引
        int index = 1;
        // 出发时间
        String[] startTimes = {"8:10", "10:10", "12:30", "14:00", "16:45", "18:50", "20:30", "22:15", "19:35", "1:25", "6:00", "7:45"};
        // 到达时间
        String[] finishTimes = {"10:25", "12:40", "15:45", "16:18", "19:55", "21:10", "23:50", "23:55", "23:55", "4:55", "8:10", "10:55"};
        try {
            // 清空库
            flightPlayDao.deleteAll();
            // 插入数据
            for (int i = 0; i < citys.length; i++) {
                for (int j = 0; j < citys.length; j++) {
                    // 去除出发城市与到达城市一致的用例
                    if (i == j) {
                        continue;
                    }
                    // 出发城市与到达城市不同时间的航班计划
                    for (int k = 0; k < flights.length; k++) {
                        // 航班计划加上出发与到达日期
                        for (int l = 0; l < 5; l++) {
                            // 随机跳过
                            if (new Random().nextInt(10) % 2 == 0) {
                                continue;
                            }
                            // 随机跳过
                            if (new Random().nextInt(10) % 2 == 0) {
                                continue;
                            }
                            calendar.setTime(new Date());
                            calendar.add(Calendar.DATE, l);
                            String date = format.format(calendar.getTime());
                            // 赋值
                            FlightPlay play = new FlightPlay();
                            play.setFlightnumberid(flights[k]);
                            play.setCity(citys[i]);
                            play.setEndcity(citys[j]);
                            play.setDepartdate(date);
                            play.setArrivaldate(date);
                            play.setDeparttime(startTimes[k]);
                            play.setArrivaltime(finishTimes[k]);
                            // 保存到库
                            flightPlayDao.save(play);
                            // 增加
                            index++;
                        }
                    }
                }
            }
        } catch (Exception e) {
            return "失败:" + e.getMessage();
        }
        return "插入" + index + "数据";
    }

    @RequestMapping(value = "/specialTicket", method = RequestMethod.GET)
    @ResponseBody
    public String createSpecialTicket() {
        // 索引
        int index = 1;
        // 出发时间
        String[] pics = {"special1", "special2", "special3"};
        // 票价
        double[] moneys = {684, 758, 746, 374, 873, 362, 594, 876};
        // 航班计划
        String[] specialFlights = {"C100", "C101", "C103", "C104", "C106", "C109", "C110", "C111"};

        try {
            // 清空库
            specialTicketDao.deleteAll();
            // 插入数据
            for (int i = 0; i < citys.length; i++) {
                for (int j = 0; j < citys.length; j++) {
                    // 去除出发城市与到达城市一致的用例
                    if (i == j) {
                        continue;
                    }
                    // 出发城市与到达城市不同时间的航班计划
                    for (int k = 0; k < specialFlights.length; k++) {
                        // 航班计划加上出发与到达日期
                        for (int l = 0; l < 5; l++) {
                            // 随机跳过
                            if (new Random().nextInt(10) % 2 == 0) {
                                continue;
                            }
                            calendar.setTime(new Date());
                            calendar.add(Calendar.DATE, l);
                            String date = format.format(calendar.getTime());
                            // 查库
                            FlightPlay flightPlay = flightPlayDao.findAllByCityAndEndcityAndFlightnumberidAndDepartdate(citys[i], citys[j], specialFlights[k], date);
                            if (flightPlay == null) {
                                continue;
                            }
                            // 赋值
                            SpecialTicket ticket = new SpecialTicket();
                            ticket.setCity(citys[i]);
                            ticket.setDepartdate(date);
                            ticket.setEndcity(citys[j]);
                            switch (index) {
                                case 1:
                                    ticket.setPicture(pics[0]);
                                    break;
                                case 2:
                                    ticket.setPicture(pics[1]);
                                    break;
                                case 3:
                                    ticket.setPicture(pics[2]);
                                    break;
                                default:
                                    ticket.setPicture(pics[new Random().nextInt(2)]);
                                    break;
                            }
                            ticket.setTicketprice(moneys[k]);
                            ticket.setFlightnumberid(specialFlights[k]);
                            // 保存到库
                            specialTicketDao.save(ticket);
                            // 增加
                            index++;
                        }
                    }
                }
            }
        } catch (Exception e) {
            return "失败:" + e.getMessage();
        }
        return "插入" + index + "数据";
    }

    @RequestMapping(value = "/modifysystemtip", method = RequestMethod.GET)
    @ResponseBody
    public String createSystemTip() {
        // 索引
        int index = 1;
        //
        String[] contents = {"关于北京-迪拜航线旅客提示", "关于在线填报民航旅客健康申明的通知", "关于国航调整中国大陆至菲律宾等航线运力等的公告", "关于国航暂停运营中国大陆至意大利等航线等的公告", "关于调整国航客票免费退票的规则的提示", "关于国航调整部分中转住宿、专车接送和地空联运产品的通知", "关于发布国航涉及武汉航线客票特殊处置方法的通知"};
        try {
            // 清空库
            systemTipDao.deleteAll();
            // 随机数
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < contents.length; i++) {
                int randomInt = new Random().nextInt(20);
                list.add(-randomInt);
            }
            Collections.sort(list, Collections.reverseOrder());
            for (int i = 0; i < contents.length; i++) {
                calendar.setTime(new Date());
                calendar.add(Calendar.DATE, list.get(i));
                String date = format.format(calendar.getTime());
                // 存储
                SystemTip tip = new SystemTip();
                tip.setCreatetime(date);
                tip.setContent(contents[i]);
                tip.setDescription("systemtip" + i);
                systemTipDao.save(tip);
                // 增加
                index++;
            }
        } catch (Exception e) {
            return "失败:" + e.getMessage();
        }
        return "插入" + index + "数据";
    }


    @RequestMapping(value = "/createmember", method = RequestMethod.GET)
    @ResponseBody
    public String createMember() {
        // 索引
        int index = 1;
        //
        String[] contents = {"和风·日本，中日通航45周年", "畅游世界，多地出发，即刻出发", "添次良积，邀您参加2020新礼程", "每周四，惊喜会员日", "你买了吗？新年机票"};
        try {
            // 清空库
            userMemberZoneDao.deleteAll();
            for (int i = 0; i < contents.length; i++) {
                int randomInt = new Random().nextInt(20);
                // 保存
                UserMemberZone zone = new UserMemberZone();
                zone.setActivitydes(contents[i]);
                // 开始日期
                calendar.setTime(new Date());
                calendar.add(Calendar.DATE, -randomInt);
                zone.setStartdate(calendar.getTime());
                // 结束日期
                calendar.setTime(calendar.getTime());
                calendar.add(Calendar.DATE, 60);
                zone.setStopdate(calendar.getTime());
                // 详情描述
                zone.setPicture("member" + (i + 1));
                zone.setDesc("member" + i);
                userMemberZoneDao.save(zone);
                // 增加
                index++;
            }
        } catch (Exception e) {
            return "失败:" + e.getMessage();
        }
        return "插入" + index + "数据";
    }
}
